//
// Created by macos on 2021/12/11.
//

#include "http.h"


uint16_t ghttp_get_post(const char *url, HTTP_REQUEST_TYPE type, const char *content_type, mlist_t headers,
                        const char *request_body,
                        struct http_response *response) {
    ghttp_request *request = NULL;
    ghttp_status status;
    request = ghttp_request_new();
    if (ghttp_set_uri(request, (char *) url) == -1) {
        ghttp_clean(request);
        return CURLE_FAILED_INIT;
    }
    ghttp_set_header(request, "User-Agent", "mghttp-client/1.0 Linux/x86_64");
    ghttp_set_header(request, "Accept", "*/*");
    ghttp_set_header(request, "Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
    if (headers != NULL && list_size(headers) > 0) {
        for (int i = 0; i < list_size(headers); i++) {
            http_key_value_pair_t *kvp = (http_key_value_pair_t *) list_get(headers, i);
            ghttp_set_header(request, kvp->key, kvp->value);
        }
    }
    if (content_type != NULL)
        ghttp_set_header(request, "Content-Type", content_type);
    switch (type) {
        case HTTP_GET:
            if (ghttp_set_type(request, ghttp_type_get) == -1) {
                ghttp_clean(request);
                return CURLE_FAILED_INIT;
            }
            break;
        case HTTP_POST:
            if (ghttp_set_type(request, ghttp_type_post) == -1) {
                ghttp_clean(request);
                return CURLE_FAILED_INIT;
            }
            if (request_body != NULL) {
                uint16_t len = strlen(request_body);
                ghttp_set_body(request, (char *) request_body, len);
            }
            break;
        default:
            printf("仅支持GET/POST方法\n");
            return 3;
    }
    ghttp_prepare(request);
    status = ghttp_process(request);
    if (status == ghttp_error) {
        ghttp_request_destroy(request);
        return 2;
    }
    response->response = strdup(ghttp_get_body(request));
    response->size = ghttp_get_body_len(request);
    uint16_t http_status = ghttp_status_code(request);
    ghttp_clean(request);
    ghttp_request_destroy(request);
    ghttp_close(request);
    return http_status;

}

uint16_t ghttp_get(const char *url, mlist_t headers, mlist_t params, struct http_response *response) {
    char *url_get = (char *) url;
    if (params != NULL && list_size(params) > 0) {
        url_get = http_build_url_with_params((char *) url, params);
    }

    return ghttp_get_post(url_get, HTTP_GET, NULL, headers, NULL, response);
}


uint16_t ghttp_post(const char *url, mlist_t headers, mlist_t params, struct http_response *response) {
    char *post_body = "";
    if (params != NULL && list_size(params) > 0) {
        post_body = http_build_params(params);
    }
    return ghttp_get_post(url, HTTP_POST, HTTP_CONTENT_TYPE_XFORM, headers, post_body, response);
}

uint16_t ghttp_post_json(const char *url, mlist_t headers, json_doc *json, struct http_response *response) {
    char *post_body = "";
    if (json != NULL && json_doc_get_root(json) != NULL) {
        post_body = json_write(json, JSON_WRITE_NOFLAG, NULL);
    }
    return ghttp_get_post(url, HTTP_POST, HTTP_CONTENT_TYPE_JSON, headers, post_body, response);
}


uint16_t ghttp_post_xml(const char *url, mlist_t headers, const char *xml, struct http_response *response) {
    return ghttp_get_post(url, HTTP_POST, HTTP_CONTENT_TYPE_XML, headers, xml, response);
}
